package com.xiaodou.mjf.web.console.audit.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaodou.mjf.application.core.manager.FinancingManager;
import com.xiaodou.mjf.application.core.repository.mybatis.LoanMapper;
import com.xiaodou.mjf.application.core.service.FinancingAttributeService;
import com.xiaodou.mjf.application.core.service.FinancingLoanService;
import com.xiaodou.mjf.application.core.service.FinancingService;
import com.xiaodou.mjf.application.fee.manager.FeeManager;
import com.xiaodou.mjf.entity.core.Financing;
import com.xiaodou.mjf.entity.core.FinancingLoan;
import com.xiaodou.mjf.entity.core.Loan;
import com.xiaodou.mjf.entity.fee.AccountFee;
import com.xiaodou.mjf.infrastructure.common.controller.ApplicationController;

/**
 * 满标放款Controller
 * 
 * @author BILLY
 *
 */
@Controller
@RequestMapping("audit/fullscale/loan")
public class FullScaleLoanController extends ApplicationController {

	@Resource
	FinancingService financingService;
	@Resource
	FinancingLoanService financingLoanService;
	@Resource
	FinancingAttributeService financingAttributeService;
	@Resource
	FinancingManager financingManager;
	@Resource
	LoanMapper loanMapper;
	@Resource
	FeeManager feeManager;
	@RequestMapping("")
	public String apply(Loan loan) {

		return "audit/fullscaleloan/list";
	}

	@RequestMapping(value = { "data.json" })
	@ResponseBody
	public Map<String, Object> data(Financing financing) {
		try {
			financing.setInvestStatus(Financing.INVEST_STATUS_FOR_LOAN);
			financing.setType(Financing.TYPE_XIAODOU);
			financing.setStatus(Financing.NORMAL);
			Page<Financing> page = financingService.pageFullScale(financing);
			return page(page);
		} catch (Exception ex) {
			logger.error(ex.toString());
			throw ex;
		}
	}

	
	@RequestMapping(value = { "form", "form/{id}" }, method = RequestMethod.GET)
	public String form(Model model, @PathVariable String id) {
			model.addAttribute("financingId", id);
		return "audit/fullscaleloan/form";
	}
	
    
    @RequestMapping(value = {"loanData.json", "loanData.json/{id}"})
    @ResponseBody
    public Map<String,Object> tranData(Loan loan, @PathVariable String id){
    	loan.setFinancingId(id);
    	loan.setStatus(Loan.NORMAL);
    	loan.setFinanceStatus(Financing.INVEST_STATUS_FOR_LOAN);
    	if(loan.getStart()==1) {
    		loan.setStart(0);
		} else {
			loan.setStart(loan.getStart()-1);
		}
		Integer count = loanMapper.getTotalCount(loan);
    	List<Loan> loanList = loanMapper.getLoanByFinancingId(loan);
    	return page(count, loanList);
    }
	
    
	@RequestMapping(value = { "commit", "commit/{id}" })
	@ResponseBody
	public boolean audit(@PathVariable String id) {
		boolean flag = false;
		try {
			feeManager.payFullScale(id);
			flag = true;
		} catch (Exception ex) {
			flag = false;
			logger.error(ex.toString());
		}
		return flag;
	}

}
